/**
 * 
 */
package com.gwcloud.leetcode.hot100;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * @ClassName   : Leetcode0128 
 * @author      : ganwei 
 * @date        : 2020年5月20日
 * @Description : Leetcode0128 最长连续序列
 */
public class Leetcode0128 {
    public int longestConsecutive(int[] nums) {
    	Arrays.sort(nums);
    	int max = 0;
    	int curmax = 1;

    	for(int i = 0; i < nums.length-1; i++){
    		if(nums[i] != nums[i+1]){
    			if(nums[i]+1 == nums[i+1]){
    				curmax++;
    			}else{
    				max = Math.max(max, curmax);
    				curmax = 1;
    			}
    		
    		}
    	}
    	return Math.max(max,curmax);
    }
    
    public int longestConsecutive_1(int[] nums){
    	Set<Integer> num_set = new HashSet<Integer>();
    	for(int i : nums){
    		num_set.add(i);
    	}
    	int max = 0;
    	for(int num: num_set){
    		if(!num_set.contains(num-1)){
    			int currentNum = num;
    			int curmax = 1;
    			
    			while(num_set.contains(currentNum+1)){
    				curmax += 1;
    				currentNum += 1;
    			}
    			max = Math.max(curmax, max);
    		}
    		
    	}
    	return max;
    }
    
    public static void main(String[] args){
    	Leetcode0128 l0128 = new Leetcode0128();
    	int[] nums = {0, -1};
    	System.out.println(l0128.longestConsecutive_1(nums));
    }
}
